package com.youdianstar.app.common.widget.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.youdianstar.app.common.R
import com.youdianstar.app.common.entity.SheetData
import com.youdianstar.app.common.widget.SheetCheckHelper


/**
 * File: BottomSheetAdapter
 * Author: 82149 Create: 2021/10/11 16:21
 * Changes (from 2021/10/11)
 * --------------------------------------------------
 * description: 底部弹框适配器
 * ---------------------------------------------------
 *
 */
class BottomSheetAdapter(val context: Context, val datas: List<SheetData>, val selectHelper: SheetCheckHelper) : RecyclerView.Adapter<BottomSheetAdapter.SheetHolder>() {

    class SheetHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val ivIcon by lazy {
            itemView.findViewById<ImageView>(R.id.iv_icon)
        }
        val tvInfo by lazy {
            itemView.findViewById<TextView>(R.id.tv_item_name)
        }
        val tvDetails by lazy {
            itemView.findViewById<TextView>(R.id.tv_details)
        }
        val tvLine by lazy {
            itemView.findViewById<TextView>(R.id.tv_line)
        }
        val ivBox by lazy {
            itemView.findViewById<ImageView>(R.id.sheet_item_box)
        }

        fun setIcon(resId: Int) {
            if (resId == 0) {
                ivIcon.visibility = View.GONE
            } else {
                ivIcon.visibility = View.VISIBLE
                ivIcon.setImageResource(resId)
            }
        }

        fun setContent(text: String) {
            tvInfo.text = text
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SheetHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.layout_item_sheet, parent, false)
        return SheetHolder(view)
    }

    override fun onBindViewHolder(holder: SheetHolder, position: Int) {
        holder.setIcon(datas[position].resId)
        val str: String = if (datas[position].content == null) "" else datas[position].content!!
        holder.setContent(str)

        holder.tvLine.visibility = if (position!=itemCount-1) View.VISIBLE else View.INVISIBLE
        holder.ivBox.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.selector_box_gou))
        selectHelper.bindViewHolder(holder)
    }

    override fun getItemCount(): Int {
        return datas.size
    }

}